package com.im.partchat.ui.activity;


import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.flyou.player.media.IjkPlayerView;
import com.flyou.player.utils.NetWorkUtils;
import com.im.partchat.R;
import com.im.partchat.base.BaseActivity;
import com.im.partchat.bean.learn.Tab4New;
import com.im.partchat.utils.Navigation;

import butterknife.BindView;


public class VideoDetailActivity extends BaseActivity {
    @BindView(R.id.player_view)
    IjkPlayerView playerView;
    private Tab4New movie;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video_detail;
    }

    @Override
    protected void initData() {
        movie = (Tab4New) getIntent().getSerializableExtra(Navigation.BEANMOVIE);
        if (movie == null || TextUtils.isEmpty(movie.getUrl())) {
            showToast("视频地址有误");
            return;
        }

        Glide.with(this).load(movie.getImg()).centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).into(playerView.mPlayerThumb); // Show the thumb before play

        playerView.init()
                .setTitle(movie.getTitle())
                .alwaysFullScreen()
                .isTvLive(false)
                .setVideoPath(movie.getUrl());

        if (NetWorkUtils.isNetworkAvailable(getApplicationContext())) {
            if (NetWorkUtils.isWifiConnected(getApplicationContext())) {
                playerView.start();
            } else {
                showToast("您当前处于移动网络，点击屏幕开始播放，请注意流量消耗");
            }
        } else {
            showToast("网络未连接，请检查网络设置");
        }
    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {

    }

    protected void onResume() {
        super.onResume();
        playerView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        playerView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playerView.onDestroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        playerView.configurationChanged(newConfig);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (playerView.handleVolumeKey(keyCode)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (playerView.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

}
